package com.e3.toolkit;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.Template;

public class TransferUtils {
	
	private static Configuration cfg;
	
	public static void transfer(String ftl,String outFile,SimpleHash paras){
		init();
		try {
			
			Template entityTemp = cfg.getTemplate(ftl);
						
			File file = new File(outFile);
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			Writer writer = new FileWriter(file);
			entityTemp.process(paras, writer);
			writer.flush();
			writer.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void transfer(String path,String packageName,String className,TableMeta meta){
		init();
		try {
			String ftlName = "Entity.ftl";
			if(meta.isBillEntity()){
				ftlName = "EntityBill.ftl";
			}
			else if(meta.isOCEntity()){
				ftlName = "EntityOC.ftl";
			}
			Template entityTemp = cfg.getTemplate(ftlName);
			
			SimpleHash sh = new SimpleHash();
			sh.put("TableMeta",meta);
			sh.put("packageName", packageName);
			sh.put("className", className);
			
			if(path.lastIndexOf("/")<0 && path.lastIndexOf("\\")<0){
				path = path+"/";
			}
			String truePath = path+"/"+packageName.replace('.', '/');
			
			File file = new File(truePath + "/entity/"+className+"Entity.java");
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			Writer writer = new FileWriter(file);
			entityTemp.process(sh, writer);
			writer.flush();
			writer.close();
			
//			file = new File(truePath + "/vo/"+className+".java");
//			if(!file.getParentFile().exists()){
//				file.getParentFile().mkdirs();
//			}
//			writer = new FileWriter(file);
//			Template voTemp = cfg.getTemplate("VO.ftl");
//			voTemp.process(sh, writer);
//			writer.flush();
//			writer.close();
//			
//			file = new File(truePath + "/vo/"+className+"Fields.java");
//			if(!file.getParentFile().exists()){
//				file.getParentFile().mkdirs();
//			}
//			writer = new FileWriter(file);
//			Template vfTemp = cfg.getTemplate("VOFields.ftl");
//			vfTemp.process(sh, writer);
//			writer.flush();
//			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void init(){
		if(cfg != null){
			return;
		}
		
		cfg = new Configuration();
		try {
			URL url = TransferUtils.class.getResource(".");
			File file = new File(url.getFile());
			cfg.setDirectoryForTemplateLoading(file);
			cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public static void main(String[] args){
		String path = new File(".").getAbsolutePath();
		path = path.substring(0,path.indexOf("Tools"));
		System.out.println(path);
	}
}
